package kameleon.document;

import java.util.Iterator;

import kameleon.exception.InvalidIndexException;


/**
 * Represents the document with all its information.
 * 
 * <p>A document consists of a list of paragraphs and document properties.
 * The paragraphs are stored in the same order as the would appear in a
 * normal document.
 * 
 * @author		Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * @version		1.0
 * 
 * @see			ElementPropertiesDefaultNames
 */
public class Document extends ElementProperties implements Iterable<Paragraph> {

	/**
	 * Needed to serialize this class.
	 * 
	 * @see		java.io.Serializable
	 */
	private static final long serialVersionUID = -6131456922672427911L ;

	/**
	 * Saves the content of this instance of {@code Document}.
	 */
	protected Paragraphs paragraphs ;

	/**
	 * Empty constructor.
	 * 
	 * <p>Builds an instance without any properties or paragraphs.
	 */
	public Document() {
		this(new Paragraphs()) ;
	}// Document()

	/**
	 * Builds an instance with the given paragraphs.
	 * 
	 * @param	ps
	 * 			instance of {@code Paragraphs} containing the 
	 * 			paragraphs for this document
	 */
	public Document(Paragraphs ps) {
		super() ;
		this.paragraphs = ps ;
	}// Document(Paragraphs)

	/**
	 * Add an instance of {@code Paragraph} at the specified index.
	 * 
	 * @param 	p
	 * 			instance of {@code Paragraph} added
	 * 
	 * @param 	index
	 * 			insert position
	 * 
	 * @throws	InvalidIndexException
	 * 			if the given index is out of range
	 * 			{@code index < 0 || index >= getCount()}
	 * 
	 * @see		Paragraphs#add(Paragraph, int)
	 */
	public void add(Paragraph p, int index) throws InvalidIndexException {
		this.paragraphs.add(p, index) ;
	}// add(Paragraph, int)

    /**
     * Prepends an instance of {@code Paragraph}.
	 * 
	 * @param 	p
	 * 			instance of {@code Paragraph} prepended
	 * 
	 * @see		Paragraphs#push(Paragraph)
     */
    public void push(Paragraph p) {
    	this.paragraphs.push(p) ;
    }// push(Paragraph)

	/**
	 * Appends an instance of {@code Paragraph}.
	 * 
	 * @param 	p
	 * 			instance of {@code Paragraph} appended
	 * 
	 * @see		Paragraphs#append(Paragraph)
	 */
	public void append(Paragraph p) {
		this.paragraphs.append(p) ;
	}// append(Paragraph)

	/**
	 * Removes the instance of {@code Paragraph} found at the given index.
	 * 
	 * @param 	index
	 * 			index of the removed {@code Paragraph}
	 * 
	 * @throws	InvalidIndexException
	 * 			if the given index is out of range
	 * 			{@code index < 0 || index >= getCount()}
	 * 
	 * @see		Paragraphs#remove(int)
	 */
	public void remove(int index) throws InvalidIndexException {
		this.paragraphs.remove(index) ;
	}// remove(int)

	/**
	 * Removes all the instances matching the given {@code Paragraph}.
	 * 
	 * @param 	p
	 * 			removed {@code Paragraph}
	 * 
	 * @see		Paragraphs#remove(Paragraph)
	 */
	public void remove(Paragraph p) {
		this.paragraphs.remove(p) ;
	}// remove(Paragraph)

	/**
	 * Returns the number of instances of {@code Paragraph} contained in this 
	 * {@code Document}.
	 * 
	 * @return	Number of instances of {@code Paragraph} contained in this
	 * 			{@code Document}
	 */
	public int getCount() {
		return this.paragraphs.getCount() ;
	}// getCount()

	/**
	 * Returns an iterator for this instance.
	 * 
	 * <p>The iterator iterates over the document sequentially.
	 * 
	 * @return	an instance of {@code Iterator<Paragraph>} for this instance
	 */
	@Override
	public Iterator<Paragraph> iterator() {
		return this.paragraphs.iterator() ;
	}// iterator()
	
	/**
	 * Returns all the paragraphs of this instance.
	 * 
	 * @return	instance of {@code Paragraphs} containing all the paragraphs
	 */
	public Paragraphs getParagraphs() {
		return this.paragraphs ;
	}// getParagraphs()

}// class Document